
#include <OpenGLES/ES1/gl.h>
#include <OpenGLES/ES1/glext.h>

#include "Painter.h"
#include "SB.h"

Painter::Painter()
{
}

Painter::~Painter()
{
}

void Painter::drawTexturedQad(float width, float height, const Texture* text, Vector4 color)
{
    drawTexturedQad(0, 0, width, height, 0, 0, 1, 1, text, color);
}

void Painter::drawTexturedQad(float x, float y, float width, float height, const Texture* text, Vector4 color)
{
    drawTexturedQad(x, y, width, height, 0, 0, 1, 1, text, color);
}

float getRotateX(float _x, float _y, float _fCos, float _fSin)
{
    return _x * _fCos - _y * _fSin;
}

float getRotateY(float _x, float _y, float _fCos, float _fSin)
{
    return _x * _fSin + _y * _fCos;
}

void Painter::drawTexturedQad(float x, float y, float width, float height, float u0, float v0, float u1, float v1, const Texture* text, Vector4 color)
{
    drawTexturedQad(x, y, width, height, u0, v0, u1, v1, 0, text, color);
}

void Painter::drawTexturedQad(float x, float y, float width, float height, float u0, float v0, float u1, float v1, float _fAngle, const Texture* text, Vector4 color)
{
    drawTexturedQad(x, y, 0, 0, width, height, u0, v0, u1, v1, _fAngle, text, color);
}

void Painter::drawTexturedQad(float x, float y, float _xCenter, float _yCenter, float width, float height, float u0, float v0, float u1, float v1, float _fAngle, const Texture* text, Vector4 color)
{
    // adapt variables to our pixels resolutions
    x *= SB::PIXHEIGHT;
    y *= SB::PIXWIDTH;
    
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	text->activate();
	
    float fCos = cos(_fAngle);
    float fSin = sin(_fAngle);
    
//    x -= _xCenter;
//    y -= _yCenter;
    
    float w0 = -_xCenter * width;
    float h0 = -_yCenter * height;
    float w1 = width - (_xCenter * width);
    float h1 = height - (_yCenter * height);

	float verts[] = {	
        x + getRotateX(w0, h0, fCos, fSin), y +	getRotateY(w0, h0, fCos, fSin), 0.0,
        x + getRotateX(w1, h0, fCos, fSin), y + getRotateY(w1, h0, fCos, fSin),	0.0,
        x + getRotateX(w0, h1, fCos, fSin),	y + getRotateY(w0, h1, fCos, fSin),	0.0,
        x + getRotateX(w1, h1, fCos, fSin), y + getRotateY(w1, h1, fCos, fSin),	0.0 };

	float	coords[] = { 
		u0, v1,
		u1,	v1,
		u0,	v0,
		u1,	v0 };
	
    //glRotatef(_fAngle, 0, 0, -1);
	glColor4f(color[0] * color[3], color[1] * color[3], color[2] * color[3], color[3]);
    glVertexPointer(3, GL_FLOAT, 0, verts);
	glTexCoordPointer(2, GL_FLOAT, 0, coords);
	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
